/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.Usuario_cont;
import javax.swing.table.DefaultTableModel;
import model.Usuario;

/**
 *
 * @author Diego
 */
public class JInternalFrame_usuario_cont extends javax.swing.JInternalFrame {

    /**
     * Creates new form JInternalFrame_usuario
     */
    public JInternalFrame_usuario_cont() {
        initComponents();
    }

    public void listar() {
        modelo = (DefaultTableModel) Tabela_usuario.getModel();
        modelo.setNumRows(0);
        usuario_cont = new Usuario_cont();
        for (int i = 0; i < usuario_cont.read().size(); i++) {
            modelo.addRow(new Object[]{usuario_cont.read().get(i).getIdusuario(),
                        usuario_cont.read().get(i).getNome(),
                        usuario_cont.read().get(i).getEmail()});
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Tabela_usuario = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txt_usuario = new javax.swing.JTextField();
        btn_excluir = new javax.swing.JButton();
        btn_Sair = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        Tabela_usuario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id usuário", "nome", "Email"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        Tabela_usuario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Tabela_usuarioMouseClicked(evt);
            }
        });
        Tabela_usuario.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                Tabela_usuarioAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(Tabela_usuario);

        jLabel1.setText("Digite o nome do usuário para busca-lo");

        txt_usuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_usuarioKeyReleased(evt);
            }
        });

        btn_excluir.setText("Excluir");
        btn_excluir.setEnabled(false);
        btn_excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_excluirActionPerformed(evt);
            }
        });

        btn_Sair.setText("Sair");
        btn_Sair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 775, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txt_usuario))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btn_Sair)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_excluir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txt_usuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_excluir)
                    .addComponent(btn_Sair))
                .addContainerGap())
        );

        setBounds(0, 0, 811, 488);
    }// </editor-fold>//GEN-END:initComponents

    private void btn_SairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SairActionPerformed
        dispose();
    }//GEN-LAST:event_btn_SairActionPerformed

    private void btn_excluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_excluirActionPerformed
        modelo = (DefaultTableModel) Tabela_usuario.getModel();
        usuario = new Usuario();
        usuario_cont = new Usuario_cont();
        if (Tabela_usuario.getValueAt(Tabela_usuario.getSelectedRow(), 0) == null) {
            modelo.removeRow(Tabela_usuario.getSelectedRow());
        } else {
            usuario.setIdusuario(Integer.valueOf(Tabela_usuario.getValueAt(Tabela_usuario.getSelectedRow(), 0).toString()));
            usuario_cont.delete(usuario);
            listar();
        }
        btn_excluir.setEnabled(false);
    }//GEN-LAST:event_btn_excluirActionPerformed

    private void Tabela_usuarioAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_Tabela_usuarioAncestorAdded
        listar();
    }//GEN-LAST:event_Tabela_usuarioAncestorAdded

    private void Tabela_usuarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Tabela_usuarioMouseClicked
        btn_excluir.setEnabled(true);
    }//GEN-LAST:event_Tabela_usuarioMouseClicked

    private void txt_usuarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_usuarioKeyReleased
        modelo = (DefaultTableModel) Tabela_usuario.getModel();
        modelo.setNumRows(0);
        usuario = new Usuario();
        usuario_cont = new Usuario_cont();
        usuario.setNome(txt_usuario.getText());
        if (txt_usuario.getText().equals("")) {
            listar();
            btn_excluir.setEnabled(false);
        } else {
        for (int i = 0; i < usuario_cont.search(usuario).size(); i++) {
            modelo.addRow(new Object[]{usuario_cont.search(usuario).get(i).getIdusuario(),
                        usuario_cont.search(usuario).get(i).getNome(),
                        usuario_cont.search(usuario).get(i).getEmail()});
         }
        }
    }//GEN-LAST:event_txt_usuarioKeyReleased
    private DefaultTableModel modelo;
    private Usuario usuario;
    private Usuario_cont usuario_cont;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tabela_usuario;
    private javax.swing.JButton btn_Sair;
    private javax.swing.JButton btn_excluir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txt_usuario;
    // End of variables declaration//GEN-END:variables
}
